package com.xqiapp.jsp.config;


import com.alibaba.druid.pool.DruidDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by rwrwd7 on 2017/9/22.
 */
public class DataSourceFactory {

    private static final String TAG = "DataSourceFactory";

    private static Properties config = null;

    private static Map<String, String> configMap = new HashMap<String, String>();

    private static String FILE_NAME = "application.properties";

    static {
        synchronized (DataSourceFactory.class) {
            InputStream in = DataSourceFactory.class.getClassLoader().getResourceAsStream(FILE_NAME);
            config = new Properties();
            try {
                config.load(in);
                in.close();
                for (Iterator<String> iterator = getStringKeys().iterator(); iterator.hasNext();) {
                    String key =  iterator.next();
                    configMap.put(key, getStringValue(key));
                }
            } catch (IOException e) {
                System.out.println(new Date()+" : "+TAG+" : Not found application.properties defined error : "+e.getMessage());
                e.printStackTrace();
            }catch (Exception e) {
                System.out.println(new Date()+" : "+TAG+" : "+e.getMessage());
                e.printStackTrace();
            }
        }
    }

    public static Map<String, String> getConfigMap() {
        return configMap;
    }

    public static Set<Object> getKeys(){
        return config.keySet();
    }

    public static Set<String> getStringKeys(){
        return config.stringPropertyNames();
    }

    public static Collection<Object> getValues(){
        return config.values();
    }

    public static String getStringValue(String key){
        return config.getProperty(key);
    }

    public static Object getValue(String key) {
        return config.get(key);
    }


    public static String dataSourceType = "type";
    public static String username = "username";
    public static String password = "password";
    public static String url = "url";
    public static String driverClassName = "driverClassName";
    public static String filters = "filters";
    public static String maxActive = "maxActive";
    public static String initialSize = "initialSize";
    public static String maxWait = "maxWait";
    public static String minIdle = "minIdle";
    public static String timeBetweenEvictionRunsMillis = "timeBetweenEvictionRunsMillis";
    public static String minEvictableIdleTimeMillis = "minEvictableIdleTimeMillis";
    public static String validationQuery = "validationQuery";
    public static String testWhileIdle = "testWhileIdle";
    public static String testOnBorrow = "testOnBorrow";
    public static String testOnReturn = "testOnReturn";
    public static String poolPreparedStatements = "poolPreparedStatements";
    public static String maxOpenPreparedStatements = "maxOpenPreparedStatements";

    protected static String fix = "spring.datasource.{routeKey}.";

    public static Object createDataSource(Object targetTypeName) throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        String newFix = fix.replace("{routeKey}", targetTypeName + "");
        dataSource.setDriverClassName(getStringValue( newFix + driverClassName));
        dataSource.setUsername(getStringValue( newFix + username));
        dataSource.setPassword(getStringValue( newFix + password));
        dataSource.setUrl(getStringValue( newFix + url));
        dataSource.setInitialSize(5);
        dataSource.setMinIdle(1);
        dataSource.setMaxActive(10);// 启用监控统计功能
        dataSource.setFilters("stat");//设置是否显示sql语句
        return dataSource;
    }


}
